#include "hwboard.h"

SPIClass fspi(PB5, PB4, PB3);
uint8_t lcd_pwr=PE2;
uint8_t lcd_cs=PD7;
FlashFs flash_fs;
#include <IWatchdog.h>
uint8_t di[] = {X1, X2, X3}, di_nrs = sizeof(di);
hwboard::hwboard()
{
    extern int VECTOR_ISR;
    SCB->VTOR = (uint32_t)&VECTOR_ISR;
}
void hwboard::baseinit()
{
    flash_fs.begin(&fspi, PA15);
    flash_fs.dir();
}
void hwboard::begin()
{
    io_init();

    pinMode(LED_RUN, OUTPUT);
    pinMode(LED_ERR, OUTPUT);
    pinMode(LED_ETH, OUTPUT);
    digitalWrite(LED_ERR, HIGH);
    digitalWrite(LED_RUN, HIGH);
    digitalWrite(LED_ETH, HIGH);

    atemp_init = analogRead(ATEMP);
};
void hwboard::io_init()
{
    analogReadResolution(12);
}
#ifdef BUILD_NUMBER
const int build = BUILD_NUMBER;
#else
const int build = 0;
#endif
int uptime;
int atemp_init, atemp, avref;
IOVAR io;
uint16_t  wdg_time;
uint8_t  wdg_en;

void hwboard::input()
{
    io.xx1 = digitalRead(X1) ? 0 : 1;
    io.xx2 = digitalRead(X2) ? 0 : 1;
    io.xx3 = digitalRead(X3) ? 0 : 1;
}
void hwboard::output()
{
}
extern bool PLC_shutdown;

void hwboard::flashClear()
{
    PLC_shutdown = 1;
    flash_fs.clear();
    reboot = 1;
    while (1)
        ;
}
hwboard board;

int test_index;
extern "C" void OutputIndex(int line, const char *msg)
{
    test_index++;
    digitalWrite(LED_ERR, HIGH);
    digitalWrite(LED_RUN, HIGH);
    digitalWrite(LED_ETH, HIGH);
    switch (test_index / 10)
    {
    case 0:
        break;
    case 1:
        digitalWrite(LED_RUN, LOW);
        break;
    case 2:
        digitalWrite(LED_ETH, LOW);
        break;
    case 3:
        digitalWrite(LED_ERR, LOW);
        break;
    default:
        digitalWrite(LED_ERR, LOW);
        digitalWrite(LED_RUN, LOW);
        digitalWrite(LED_ETH, LOW);
        break;
    }
    core_debug("\nTEST %d : %s , L%d\n", test_index, msg, line);
}
const char *hwModel = "hwboard";
extern "C"
{
    void plc_run_start()
    {
        digitalWrite(LED_RUN, LOW);
    }
    void plc_run_end()
    {
        digitalWrite(LED_RUN, HIGH);
    }
    void net_led_on()
    {
        digitalWrite(LED_ETH, LOW);
    }
    void net_led_off()
    {
        digitalWrite(LED_ETH, HIGH);
    }
}
